<?php

include_once('connexion.php');
$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'index.php';

if(isset($_POST['new_wall_send']))
{
	$text = htmlentities($_POST['new_wall_message']);
	$message = new Message(array(
		'contest_id' => $_POST['new_wall_contest'],
		'responseMessage_id' => '',
		'messageFrom' => $userConnected->id(),
		'mailFrom' => $userConnected->mail(),
		'messageTo' => '',
		'mailTo' => '',
		'subject' => '',
		'text' => $text,
		'category_id' => 5,
		'sendMail' => 0
		));
	$messageManager->add($message);
	header('Location: '.$referer);
}
if(isset($_POST['comment_wall_send']))
{
	$text = htmlentities($_POST['comment_wall_message']);
	$message = new Message(array(
		'contest_id' => $_POST['comment_wall_contest'],
		'responseMessage_id' => $_POST['comment_wall_responseMessage'],
		'messageFrom' => $userConnected->id(),
		'mailFrom' => $userConnected->mail(),
		'messageTo' => '',
		'mailTo' => '',
		'subject' => '',
		'text' => $text,
		'category_id' => 5,
		'sendMail' => 0
		));
	$messageManager->add($message);
	header('Location: '.$referer);
}

if(isset($_POST['seeEvents']) || isset($_POST['seeEvents2']))
{
	if($_POST['seeEvents'] != "") header('Location: contest.php?contestid='.$_POST['contestid'].'&view=events&seeEvents='.$_POST['seeEvents']);
	if($_POST['seeEvents2'] != "") header('Location: contest.php?contestid='.$_POST['contestid'].'&view=events&seeEvents='.$_POST['seeEvents2']);
}

if(isset($_POST['validateBet']) || isset($_POST['prevalidateBet']) || isset($_POST['validateBets']) || isset($_POST['prevalidateBets']))
{
	if(isset($_POST['prevalidateBet']))
	{
		$scheduleEvents = array(key($_POST['prevalidateBet']));
		$status = 1;
	}
	if(isset($_POST['validateBet']))
	{
		$scheduleEvents = array(key($_POST['validateBet']));
		$status = 2;
	}
	if(isset($_POST['prevalidateBets']) && isset($_POST['scheduleEvents']))
	{
		$scheduleEvents = unserialize($_POST['scheduleEvents']);
		$status = 1;
	}
	if(isset($_POST['validateBets']) && isset($_POST['scheduleEvents']))
	{
		$scheduleEvents = unserialize($_POST['scheduleEvents']);
		$status = 2;
	}
	switch($_POST['sport'])
	{
		case 1: //C'est du basket
			foreach($scheduleEvents as $scheduleEvent_id)
			{
				if(isset($_POST['winningTeam_'.$scheduleEvent_id.''])) $winningTeam = $_POST['winningTeam_'.$scheduleEvent_id.''];
				else $winningTeam = null;
				if($winningTeam == null || $winningTeam != 1 && $winningTeam != 2) continue;
				if(isset($_POST['pointsDifference_'.$scheduleEvent_id.''])) $pointsDifference = 1;
				else $pointsDifference = 0;
				if(isset($_POST['pointsTotal'])) $pointsTotal = 1;
				else $pointsTotal = 0;
				if(isset($_POST['overtime_'.$scheduleEvent_id.''])) $overtime = 1;
				else $overtime = 0;
				$bet = new BetsBasket(array(
					'status' => $status,
					'user_id' => $userConnected->id(),
					'contest_id' => $_POST['contest'],
					'scheduleEvent_id' => $scheduleEvent_id,
					'dateBet' => date("Y/m/d h:i:s"),
					'winningTeam' => $winningTeam,
					'pointsDifference' => $pointsDifference,
					'pointsTotal' => $pointsTotal,
					'overtime' => $overtime
				));
				$betsManager->validate($bet, $status);
				}
			break;
		default:
			break;
	}
	header('Location: '.$referer);
}

?>